/**
 *
 *  @auth xlx_good@qq.com
 *  @date 17/6/21.
 *
 */

requirejs.config({
    baseUrl: '../../',
    paths:  {
        avalon: 'javascript/avalon.min',
        jquery: 'js/plugins/jquery/jquery-2.1.1.min',
        pagination: 'lib/pagination/jquery.pagination',
        WdatePicker: 'js/plugins/DatePicker/WdatePicker',
        config: 'javascript/config',
        api: 'javascript/api',
        layer: 'js/plugins/layer1/layer',
        arg: 'javascript/arg'
    },
    shim: {
        pagination: {
            deps: ['jquery']
        },
        api: {
            deps: ['jquery']
        },
        layer: {
            deps: ['jquery']
        }
    }
});


(function(win, dom, requirejs, initVm){
    var vm;
    requirejs(['avalon', 'jquery', 'pagination', 'WdatePicker', 'config', 'api', 'layer','arg'], function(){
        //初始化vm
        vm = initVm(dom);
    });

})(window, document, requirejs, function(dom){
    var vm = avalon.define({
        $id: 'container',
        form:{
            startOrderTime: '',
            endOrderTime: '',
            code: '',
            buyerName: '',
			pageNum: 1,
			pageSize: 10
        },
		keyword: '',
		keywordList: {
			"code": "子订单号",
			"buyerName": "采购商名称"
		},
		list: [],
        search: function(){
			for(var key in vm.keywordList) {
				vm.form[key] = null;
			}
			vm.form[$('#selKeyword').val()] = vm.keyword;
			Api.orderForSendPage(vm.form, function(resp) {
				if(resp.code == 0) {
					if(vm.form.pageNum == 1) {
						setPagination(resp.data.total, resp.data.pageNum - 1, vm);
					}
					vm.list = resp.data.list;
				} else {
					layer.msg(resp.msg);
				}
			});
        },
		orderSend: function(orderCode) {
			// var form = {
			// 	code: orderCode
			// }
			Api.orderSend(orderCode, function(resp) {
				if(resp.code == 0) {
					layer.msg(resp.msg);
					vm.search();
				} else {
					layer.msg(resp.msg);
				}
			});
		},
        showLayer: function(code) {
            layer.open({
                title: '发货描述',
                type: 1,
                area: ['400px','210px'],
                shadeClose: false,
                content: '<div class="sendLayer"><textarea id="sendDesc" placeholder="请输入物流公司与货单号（或送货司机电话）"></textarea></div>',
                btn: ['确定', '取消'],
                yes: function(index,layero) {
                    var params = {
                        sendDesc: $('#sendDesc').val(),
                        code: code
                    }
                    vm.orderSend(params);
                    layer.close(index);
                }
            })
        },
    })

    avalon.filters.filterStatus = function(status) {
        return orderSubStatusMap[status];
    }

    avalon.filters.filterOption = function(el){
        var params = {
            code: el.code
        }
        return '<a href="'+Arg.url("./detail.html", params)+'">详情</a>';
    }

    avalon.scan(dom.body);
	vm.search();
    return vm;
});

/**
 * 设置翻页
 * @param <number> 总条数
 * @param <number> 当前页码
 * */
function setPagination(total, current, vm){
    $('.pagination').pagination(total,{
        items_per_page: 10, //  每页显示条数
        current_page: current,
        num_display_entries: 20, // 显示的页面个数
        next_text: '下一页',
        prev_text: '上一页',
        num_edge_entries: 2, //省略号后面显示的页码个数
        callback: function(index){
			if(vm.form.pageNum != (index + 1)){
				vm.form.pageNum = index + 1;
			    vm.search();
			}
        }
    });
}
